package wcg;

import ikelib.NumericTextField;

import javax.swing.JPanel;
import java.awt.Frame;
import java.awt.BorderLayout;
import javax.swing.JDialog;
import javax.swing.JCheckBox;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Rectangle;
import javax.swing.JLabel;
import java.awt.Dimension;
import javax.swing.JComboBox;

import GraphicView.GraphicView;


public class DialogSingleChannelAmplitudeProperty extends JDialog {

	private static final long serialVersionUID = 1L;
	public static final int UnitModeDigit = 0;
	public static final int UnitModePU = 1;
	
	private JPanel jContentPane = null;
	private JCheckBox jCheckBoxConvUnit = null;
	private JPanel jPanel1 = null;
	private NumericTextField jTextFieldFactor = null;
	private JLabel jLabel1 = null;
	private JTextField jTextFieldPUText = null;
	private JLabel jLabel2 = null;
	private JTextField jTextFieldChID = null;
	private JLabel jLabel3 = null;
	public JButton jButtonOK = null;
	public JButton jButtonCancel = null;
	
	public  double 	Factor;
	public  String 	UnitText;
	public 	int		ConvMode;
	public	int		ChID;
	public 	long	FullScale=8388607;
	
	private  GraphicView Parent;	// @
	
	private JLabel jLabel11 = null;
	//private JTextField jTextFieldFullScale = null;
	private NumericTextField jTextFieldFullScale = null;
	public void setparam(){
		String cv = new String();
		cv = String.format("%g", Factor);
		jTextFieldFactor.setText(cv);
		jTextFieldPUText.setText(UnitText);
		jTextFieldChID.setText(String.format("%04X", ChID));
		switch(ConvMode){
		case UnitModePU:	// Convert Physical Unit
			jCheckBoxConvUnit.setSelected(true);
			break;
		default:
			jCheckBoxConvUnit.setSelected(false);
			break;
		}
		jTextFieldFullScale.setText(String.format("%,d",FullScale));
	}
	public void getparam(){
		try{
			String str = jTextFieldFactor.getText();
			Factor = Double.valueOf(str);
			UnitText = jTextFieldPUText.getText();
			String chid= jTextFieldChID.getText();
			if(chid.compareTo("*") == 0){
				ChID = -1;
			}
			else{
				ChID = Integer.parseInt(jTextFieldChID.getText(),16);
			}
			if(jCheckBoxConvUnit.isSelected()){
				ConvMode = UnitModePU;
			}
			else{
				ConvMode = UnitModeDigit;
			}
			double fscale=Double.valueOf(jTextFieldFullScale.getText());
			FullScale = (int)fscale;
		} catch(NumberFormatException e){
			System.err.println(e.getMessage());
		}
	}
	
	public void setParent(GraphicView owner) {
		Parent = owner;
	}
	/**
	 * @param owner
	 */
	public DialogSingleChannelAmplitudeProperty(Frame owner) {
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 245);
		this.setTitle("YScaleProperty");
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jLabel11 = new JLabel();
			jLabel11.setBounds(new Rectangle(6, 40, 95, 21));
			jLabel11.setText("FullScale [digit]");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(7, 19, 87, 16));
			jLabel3.setText("CH ID (*=all)");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(199, 105, 64, 16));
			jLabel2.setText("Unit");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(13, 102, 77, 23));
			jLabel1.setText("PQ/digit");
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getJCheckBoxConvUnit(), null);
			jContentPane.add(getJPanel1(), null);
			jContentPane.add(getJTextFieldFactor(), null);
			jContentPane.add(jLabel1, null);
			jContentPane.add(getJTextFieldPUText(), null);
			jContentPane.add(jLabel2, null);
			jContentPane.add(getJTextFieldChID(), null);
			jContentPane.add(jLabel3, null);
			jContentPane.add(getJButtonOK(), null);
			jContentPane.add(getJButtonCancel(), null);
			jContentPane.add(jLabel11, null);
			jContentPane.add(getJTextFieldFullScale(), null);
		}
		return jContentPane;
	}

	/**
	 * This method initializes jCheckBoxConvUnit	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private void itemStateChange(){
		if(jCheckBoxConvUnit.isSelected()){
			jTextFieldPUText.setEditable(true);
			jTextFieldFactor.setEditable(true);
		}
		else{
			jTextFieldPUText.setEditable(false);
			jTextFieldFactor.setEditable(false);
		}
	}
	private JCheckBox getJCheckBoxConvUnit() {
		if (jCheckBoxConvUnit == null) {
			jCheckBoxConvUnit = new JCheckBox();
			jCheckBoxConvUnit.setText("Convert into Physical Quantity (PQ)");
			jCheckBoxConvUnit.setToolTipText("Show value in phycical quantity.");
			jCheckBoxConvUnit.setBounds(new Rectangle(8, 79, 275, 24));
			jCheckBoxConvUnit.addItemListener(new java.awt.event.ItemListener() {
				public void itemStateChanged(java.awt.event.ItemEvent e) {
					itemStateChange();
				}
			});
		}
		return jCheckBoxConvUnit;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			jPanel1 = new JPanel();
			jPanel1.setLayout(new BoxLayout(getJPanel1(), BoxLayout.X_AXIS));
			jPanel1.setBounds(new Rectangle(0, 24, 0, 0));
		}
		return jPanel1;
	}

	/**
	 * This method initializes jTextFieldFactor	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldFactor() {
		if (jTextFieldFactor == null) {
			jTextFieldFactor = new NumericTextField();
			jTextFieldFactor.setToolTipText("A factor to convert digit into pysical unit(PU).Display value=factor*digit. ");
			jTextFieldFactor.setBounds(new Rectangle(9, 128, 176, 26));
		}
		return jTextFieldFactor;
	}

	/**
	 * This method initializes jTextFieldPUText	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldPUText() {
		if (jTextFieldPUText == null) {
			jTextFieldPUText = new JTextField();
			jTextFieldPUText.setToolTipText("Unit's text");
			jTextFieldPUText.setBounds(new Rectangle(192, 127, 79, 26));
		}
		return jTextFieldPUText;
	}

	/**
	 * This method initializes jTextFieldChID	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldChID() {
		if (jTextFieldChID == null) {
			jTextFieldChID = new JTextField();
			jTextFieldChID.setToolTipText("Select Channel ID. '*' means 'apply to all channel'");
			jTextFieldChID.setBounds(new Rectangle(112, 15, 75, 22));
		}
		return jTextFieldChID;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setBounds(new Rectangle(192, 171, 85, 30));
			jButtonOK.setText("OK");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getparam();
					Parent.actionPerformed(e);
				}
			});
			
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setBounds(new Rectangle(10, 174, 85, 30));
			jButtonCancel.setText("CANCEL");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					Parent.actionPerformed(e);
				}
			});
		}
		return jButtonCancel;
	}
	/**
	 * This method initializes jTextFieldFullScale	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextFieldFullScale() {
		if (jTextFieldFullScale == null) {
			jTextFieldFullScale = new NumericTextField();//new JTextField();
			jTextFieldFullScale.setToolTipText("Full scale value in digit. ex. 8388608 (24bit)");
			jTextFieldFullScale.setBounds(new Rectangle(111, 39, 103, 24));
		}
		return jTextFieldFullScale;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
